<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发送 POST 请求</title>
    <style>
        div{
            width: 400px;
            height: 400px;
            border:solid 1px #000;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
        //当鼠标移入到 div 中的时候， 向服务器发送请求 POST http://127.0.0.1/server， 将响应体的结果在 div 中呈现
        let div = document.querySelector('div');

        div.addEventListener('mouseout', function(){
            let xhr = new XMLHttpRequest();
            //设置请求行
            xhr.open('post', 'http://127.0.0.1/server');
            //设置请求头 set 设置 request 请求  header 头
            xhr.setRequestHeader('today','2022-06-01');
            xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
            //设置请求体
            xhr.send('name=zhangsan&pass=zhangsanshuai');
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    //获取响应体
                    let body = xhr.response;
                    //设置div的文本
                    div.innerHTML = body;
                }
            }
        });
        

    </script>
</body>
</html>